package com.castle.fortress.admin.flowable.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.flowable.dto.FlowableAppAuthDto;
import com.castle.fortress.admin.flowable.dto.FlowableAppDto;
import com.castle.fortress.admin.flowable.entity.FlowableAppAuthEntity;
import com.castle.fortress.admin.flowable.mapper.FlowableAppAuthMapper;
import com.castle.fortress.admin.flowable.service.FlowableAppAuthService;
import com.castle.fortress.common.enums.YesNoEnum;
import com.castle.fortress.common.utils.CommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程应用权限管理表 服务实现类
 *
 * @author castle
 * @since 2022-09-26
 */
@Service
public class FlowableAppAuthServiceImpl extends ServiceImpl<FlowableAppAuthMapper, FlowableAppAuthEntity> implements FlowableAppAuthService {
    /**
    * 获取查询条件
    * @param flowableAppAuthDto
    * @return
    */
    public QueryWrapper<FlowableAppAuthEntity> getWrapper(FlowableAppAuthDto flowableAppAuthDto){
        QueryWrapper<FlowableAppAuthEntity> wrapper= new QueryWrapper();
        if(flowableAppAuthDto != null){
            FlowableAppAuthEntity flowableAppAuthEntity = ConvertUtil.transformObj(flowableAppAuthDto,FlowableAppAuthEntity.class);
            wrapper.eq(flowableAppAuthEntity.getId() != null,"id",flowableAppAuthEntity.getId());
            wrapper.eq(flowableAppAuthEntity.getAppId() != null,"app_id",flowableAppAuthEntity.getAppId());
            wrapper.eq(flowableAppAuthEntity.getAuthId() != null,"auth_id",flowableAppAuthEntity.getAuthId());
            wrapper.eq(flowableAppAuthEntity.getAuthType() != null,"auth_type",flowableAppAuthEntity.getAuthType());
        }
        return wrapper;
    }


    @Override
    public IPage<FlowableAppAuthDto> pageFlowableAppAuth(Page<FlowableAppAuthDto> page, FlowableAppAuthDto flowableAppAuthDto) {
        QueryWrapper<FlowableAppAuthEntity> wrapper = getWrapper(flowableAppAuthDto);
        Page<FlowableAppAuthEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<FlowableAppAuthEntity> flowableAppAuthPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<FlowableAppAuthDto> pageDto = new Page(flowableAppAuthPage.getCurrent(), flowableAppAuthPage.getSize(),flowableAppAuthPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(flowableAppAuthPage.getRecords(),FlowableAppAuthDto.class));
        return pageDto;
    }


    @Override
    public List<FlowableAppAuthDto> listFlowableAppAuth(FlowableAppAuthDto flowableAppAuthDto){
        QueryWrapper<FlowableAppAuthEntity> wrapper = getWrapper(flowableAppAuthDto);
        List<FlowableAppAuthEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,FlowableAppAuthDto.class);
    }
    @Async
    @Override
    public void saveAuthAsync(FlowableAppDto flowableAppDto) {
        if(!CommonUtil.verifyParamNull(flowableAppDto,flowableAppDto.getId(),flowableAppDto.getStatus())){
            QueryWrapper<FlowableAppAuthEntity> delWrapper = new QueryWrapper<>();
            delWrapper.eq("app_id",flowableAppDto.getId());
            remove(delWrapper);
            List<FlowableAppAuthEntity> list = new ArrayList<>();
            //绑定新的关系
            if(YesNoEnum.YES.getCode().equals(flowableAppDto.getStatus())){
                if(!StrUtil.isEmpty(flowableAppDto.getAuthData())){
                    JSONObject authJson = new JSONObject(flowableAppDto.getAuthData());
                    JSONArray users = authJson.getJSONArray("user");
                    JSONArray depts = authJson.getJSONArray("dept");
                    if(users.size()>0||depts.size()>0){
                        users.forEach(item->{
                            FlowableAppAuthEntity entity= new FlowableAppAuthEntity();
                            entity.setAppId(flowableAppDto.getId());
                            entity.setAuthId(Long.parseLong(item.toString()));
                            entity.setAuthType("user");
                            list.add(entity);
                        });
                        depts.forEach(item->{
                            FlowableAppAuthEntity entity= new FlowableAppAuthEntity();
                            entity.setAppId(flowableAppDto.getId());
                            entity.setAuthId(Long.parseLong(item.toString()));
                            entity.setAuthType("dept");
                            list.add(entity);
                        });
                    }
                }
                if(list.isEmpty()){
                    FlowableAppAuthEntity entity= new FlowableAppAuthEntity();
                    entity.setAppId(flowableAppDto.getId());
                    entity.setAuthId(-1L);
                    entity.setAuthType("all");
                    list.add(entity);
                }
                saveBatch(list);
            }
        }

    }

    @Override
    public void delByAppId(List<Long> appIds) {
        if(appIds!=null && appIds.size()>0){
            QueryWrapper<FlowableAppAuthEntity> delWrapper = new QueryWrapper<>();
            delWrapper.in("app_id",appIds);
            remove(delWrapper);
        }
    }
}

